package com.yuexun.dhdoctor.module.email.presenter.impl;


import com.yuexun.dhdoctor.module.email.bean.MailBean;
import com.yuexun.dhdoctor.module.email.model.IGetModel;
import com.yuexun.dhdoctor.module.email.presenter.IGetPresenter;
import com.yuexun.dhdoctor.module.email.ui.Act_GetMailList;
import com.yuexun.dhdoctor.module.email.ui.view.IGetView;

import java.net.UnknownHostException;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by work-jx on 2016/8/1.
 */
public class GetPresenter implements IGetPresenter {
    private IGetView view;
    private IGetModel model;

    //记录Log
//    private final Logger log = ProUtil.getLogger(LoginPresneter.class);

    public GetPresenter(IGetModel model, IGetView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void getMail(Map<String, String> praise) {
        model.getmail(praise)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MailBean>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof UnknownHostException) {
                            view.PullDownRefreshState(Act_GetMailList.DOWNLOAD_NETERROR);
                        } else {
                            view.PullDownRefreshState(Act_GetMailList.DOWNLOAD_NODATA);
                        }
                    }

                    @Override
                    public void onNext(MailBean mailBean) {
                        view.showMailsList(mailBean);
                        view.PullDownRefreshState(Act_GetMailList.DOWNLOAD_SUCCESS);
                    }
                });
    }

    @Override
    public void getMail_Draft(Map<String, String> praise) {
        model.getmail_Draft(praise)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<MailBean>() {

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof UnknownHostException) {
                            view.PullDownRefreshState(Act_GetMailList.DOWNLOAD_NETERROR);
                        } else {
                            view.PullDownRefreshState(Act_GetMailList.DOWNLOAD_NODATA);
                        }
                    }

                    @Override
                    public void onNext(MailBean mailBean) {
                        view.showMailsList(mailBean);
                        view.PullDownRefreshState(Act_GetMailList.DOWNLOAD_SUCCESS);
                    }
                });
    }


}
